const user = require("../../sql/User");
const rebatesSQL = require("../../sql/Rebates");

module.exports = async function(orderData){
  const rebates = await rebatesSQL.find();
  const userSQLData = await user.find();
  const userData = [];
  orderData.forEach(val=>{
    const income = val.LocalCurrencyAmountAfterDiscount * 0.01;

    // 业务部(订单员)B提成金额=10000*25%*60%*（1-10%-20%）=1050元
    const user_OrderClerk = userSQLData.find(v=>v.employeesName===(val.OrderClerk || '奖金池')) || {team: [{}], job: {}};
    const b = rebates.find(v=>v.team==='业务部').rebates;
    userData.push({
      money: income * b * (1 - 0.1 - 0.2),
      name: val.OrderClerk || '奖金池',
      link: '业务',
      role: '员工',
      team: user_OrderClerk.team[0].tName,
      job: user_OrderClerk.job.jName,
    })
    const OrderClerkL = userSQLData.find(v=>v.team[0]?.tName===user_OrderClerk.team[0].tName && (v.job.jName==='部门经理' || v.job.jName==='分公司副经理'));
    if(OrderClerkL){
      userData.push({
        money: income * b * 0.2,
        name: OrderClerkL.employeesName,
        link: '业务',
        role: '部门经理',
        team: OrderClerkL.team[0].tName,
        job: OrderClerkL.job.jName,
      });
    }
    const OBossL = userSQLData.find(v1=>{
      return v1.team.find(v2=>v2.tName==="总经办") && v1.team.find(v2=>v2.tName===user_OrderClerk.team[0].tName);
    })
    if(OBossL){
      userData.push({
        money: income * b * 0.1,
        name: OBossL.employeesName,
        link: '业务',
        role: '总经理',
        team: OBossL.team.map(v=>v.tName),
        job: '总经理',
      })
    }

    // 市场部A提成金额=10000*25%*（1-10%-20%）=1750元
    const user_Marketer = userSQLData.find(v=>v.employeesName===(val.Marketer || '奖金池')) || {team: [{}], job: {}};
    const a = rebates.find(v=>v.team==='市场部').rebates;
    userData.push({
      money: income * a * (1 - 0.1 - 0.2),
      name: val.Marketer || '奖金池',
      link: '市场',
      role: '员工',
      team: user_Marketer.team[0].tName,
      job: user_Marketer.job.jName,
    })
    const MarketerL = userSQLData.find(v=>v.team[0]?.tName===user_Marketer.team[0].tName && (v.job.jName==='部门经理' || v.job.jName==='分公司副经理'));
    if(MarketerL){
      userData.push({
        money: income * a * 0.2,
        name: MarketerL.employeesName,
        link: '市场',
        role: '部门经理',
        team: MarketerL.team[0].tName,
        job: MarketerL.job.jName,
      })
    }
    const MBossL = userSQLData.find(v1=>{
      return v1.team.find(v2=>v2.tName==="总经办") && v1.team.find(v2=>v2.tName===user_Marketer.team[0].tName);
    })
    if(MBossL){
      userData.push({
        money: income * a * 0.1,
        name: MBossL.employeesName,
        link: '市场',
        role: '总经理',
        team: MBossL.team.map(v=>v.tName),
        job: '总经理',
      })
    }
    
    // 报价员C提成金额=10000*25%*40%*（1-10%-20%）=700元
    const user_Quoter = userSQLData.find(v=>v.employeesName===(val.Quoter || '奖金池')) || {team: [{}], job: {}};
    const c = rebates.find(v=>v.team==='报价中心').rebates;
    userData.push({
      money: income * c * (1 - 0.1 - 0.2),
      name: val.Quoter || '奖金池',
      link: '报价',
      role: '员工',
      team: user_Quoter.team[0].tName,
      job: user_Quoter.job.jName,
    })
    const QuoterL = userSQLData.find(v=>v.team[0]?.tName===user_Quoter.team[0].tName && (v.job.jName==='部门经理' || v.job.jName==='分公司副经理'));
    if(QuoterL){
      userData.push({
        money: income * c * 0.2,
        name: QuoterL.employeesName,
        link: '报价',
        role: '部门经理',
        team: QuoterL.team[0].tName,
        job: QuoterL.job.jName,
      })
    }
    const QBossL = userSQLData.find(v1=>{
      return v1.team.find(v2=>v2.tName==="总经办") && v1.team.find(v2=>v2.tName===user_Quoter.team[0].tName);
    })
    if(QBossL){
      userData.push({
        money: income * c * 0.1,
        name: QBossL.employeesName,
        link: '报价',
        role: '总经理',
        team: QBossL.team.map(v=>v.tName),
        job: '总经理',
      })
    }

    // 采购员D提成金额=10000*25%*（1-10%-20%）=1750元
    const user_Purchasing = userSQLData.find(v=>v.employeesName===(val.Purchasing || '奖金池')) || {team: [{}], job: {}};
    const d = rebates.find(v=>v.team==='采购部').rebates;
    userData.push({
      money: income * d * (1 - 0.1 - 0.2),
      name: val.Purchasing || '奖金池',
      link: '采购',
      role: '员工',
      team: user_Purchasing.team[0].tName,
      job: user_Purchasing.job.jName,
    })
    const PurchasingL = userSQLData.find(v=>v.team[0]?.tName===user_Purchasing.team[0].tName && (v.job.jName==='部门经理' || v.job.jName==='分公司副经理'))
    if(PurchasingL){
      userData.push({
        money: income * d * 0.2,
        name: PurchasingL.employeesName,
        link: '采购',
        role: '部门经理',
        team: PurchasingL.team[0].tName,
        job: PurchasingL.job.jName,
      })
    }
    const PBossL = userSQLData.find(v1=>{
      return v1.team.find(v2=>v2.tName==="总经办") && v1.team.find(v2=>v2.tName===user_Purchasing.team[0].tName);
    })
    if(PBossL){
      userData.push({
        money: income * d * 0.1,
        name: PBossL.employeesName,
        link: '采购',
        role: '总经理',
        team: PBossL.team.map(v=>v.tName),
        job: '总经理',
      })
    }

    // 仓库配货E提成金额=10000*10%*（1-10%-20%）=700元
    const user_Warehouse = userSQLData.find(v=>v.employeesName===(val.Warehouse || '奖金池')) || {team: [{}], job: {}};
    const e = rebates.find(v=>v.team==='仓库').rebates;
    userData.push({
      money: income * e * (1 - 0.1 - 0.2),
      name: val.Warehouse || '奖金池',
      link: '仓库',
      role: '员工',
      team: user_Warehouse.team[0].tName,
      job: user_Warehouse.job.jName,
    })
    const WarehouseL = userSQLData.find(v=>v.team[0]?.tName===user_Warehouse.team[0].tName && (v.job.jName==='部门经理' || v.job.jName==='分公司副经理'))
    if(WarehouseL){
      userData.push({
        money: income * e * 0.2,
        name: WarehouseL.employeesName,
        link: '仓库',
        role: '部门经理',
        team: WarehouseL.team[0].tName,
        job: WarehouseL.job.jName,
      })
    }
    const WBossL = userSQLData.find(v1=>{
      return v1.team.find(v2=>v2.tName==="总经办") && v1.team.find(v2=>v2.tName===user_Warehouse.team[0].tName);
    })
    if(WBossL){
      userData.push({
        money: income * e * 0.1,
        name: WBossL.employeesName,
        link: '采购',
        role: '总经理',
        team: WBossL.team.map(v=>v.tName),
        job: '总经理',
      })
    }

    // 供船员F提成金额=10000*5%*（1-10%-20%）=350元
    const user_ProvideBoat = userSQLData.find(v=>v.employeesName===(val.ProvideBoat || '奖金池')) || {team: [{}], job: {}};
    const f = rebates.find(v=>v.team==='供船部').rebates;
    userData.push({
      money: income * f * (1 - 0.1 - 0.2),
      name: val.ProvideBoat || '奖金池',
      link: '供船',
      role: '员工',
      team: user_ProvideBoat.team[0].tName,
      job: user_ProvideBoat.job.jName,
    })
    const ProvideBoatL = userSQLData.find(v=>v.team[0]?.tName===user_ProvideBoat.team[0].tName && (v.job.jName==='部门经理' || v.job.jName==='分公司副经理'))
    if(ProvideBoatL){
      userData.push({
        money: income * f * 0.2,
        name: ProvideBoatL.employeesName,
        link: '供船',
        role: '部门经理',
        team: ProvideBoatL.team[0].tName,
        job: ProvideBoatL.job.jName,
      })
    }
    const PBBossL = userSQLData.find(v1=>{
      return v1.team.find(v2=>v2.tName==="总经办") && v1.team.find(v2=>v2.tName===user_ProvideBoat.team[0].tName);
    })
    if(PBBossL){
      userData.push({
        money: income * f * 0.1,
        name: PBBossL.employeesName,
        link: '采购',
        role: '总经理',
        team: PBBossL.team.map(v=>v.tName),
        job: '总经理',
      })
    }

    // 财务部G提成金额=10000*5%*（1-10%-20%）=350元
    const user_Financial = userSQLData.find(v=>v.employeesName===(val.Financial || '奖金池')) || {team: [{}], job: {}};
    const g = rebates.find(v=>v.team==='财务部').rebates;
    userData.push({
      money: income * g * (1 - 0.1 - 0.2),
      name: val.Financial || '奖金池',
      link: '财务',
      role: '员工',
      team: user_Financial.team[0].tName,
      job: user_Financial.job.jName,
    })
    const FinancialL = userSQLData.find(v=>v.team[0]?.tName===user_Financial.team[0].tName && (v.job.jName==='部门经理' || v.job.jName==='分公司副经理'))
    if(FinancialL){
      userData.push({
        money: income * g * 0.2,
        name: FinancialL.employeesName,
        link: '财务',
        role: '部门经理',
        team: FinancialL.team[0].tName,
        job: FinancialL.job.jName,
      })
    }
    const FBossL = userSQLData.find(v1=>{
      return v1.team.find(v2=>v2.tName==="总经办") && v1.team.find(v2=>v2.tName===user_Financial.team[0].tName);
    })
    if(FBossL){
      userData.push({
        money: income * g * 0.1,
        name: FBossL.employeesName,
        link: '采购',
        role: '总经理',
        team: FBossL.team.map(v=>v.tName),
        job: '总经理',
      })
    }

    // 物流部H=10000*5%*(1-10%-20%)=350元
    const user_Logistics = userSQLData.find(v=>v.employeesName===(val.Logistics || '奖金池')) || {team: [{}], job: {}};
    const h = rebates.find(v=>v.team==='物流部').rebates;
    userData.push({
      money: income * h * (1 - 0.1 - 0.2),
      name: val.Logistics || '奖金池',
      link: '物流',
      role: '员工',
      team: user_Logistics.team[0].tName,
      job: user_Logistics.job.jName,
    })
    const LogisticsL = userSQLData.find(v=>v.team[0]?.tName===user_Logistics.team[0].tName && (v.job.jName==='部门经理' || v.job.jName==='分公司副经理'))
    if(LogisticsL){
      userData.push({
        money: income * h * 0.2,
        name: LogisticsL.employeesName,
        link: '物流',
        role: '部门经理',
        team: LogisticsL.team[0].tName,
        job: LogisticsL.job.jName,
      })
    }
    const LBossL = userSQLData.find(v1=>{
      return v1.team.find(v2=>v2.tName==="总经办") && v1.team.find(v2=>v2.tName===user_Logistics.team[0].tName);
    })
    if(LBossL){
      userData.push({
        money: income * h * 0.1,
        name: LBossL.employeesName,
        link: '采购',
        role: '总经理',
        team: LBossL.team.map(v=>v.tName),
        job: '总经理',
      })
    }
  })

  const staff = userData.filter(v=>v.role === "员工")
  const manager = userData.filter(v=>v.role === "部门经理")
  const boss = userData.filter(v=>v.role === "总经理")

  // 汇总员工个人数据
  const staffuData = [];
  staff.forEach(val=>{
    const i = staffuData.findIndex(v=>v.name === val.name);
    if(i !== -1){
      staffuData[i].data.push( val.money )
    }else{
      staffuData.push( {
        ...val,
        data: [ val.money ]
      } );
    }
  })
  staffuData.forEach(val=>{
    val.count = (val.data.reduce((prev,val)=>prev+val, 0)).toFixed( 2 ) - 0;
  })

  // 汇总经理数据
  const managerData = [];
  manager.forEach(val=>{
    const i = managerData.findIndex(v=>v.name === val.name);
    if(i !== -1){
      managerData[i].data.push( val.money )
    }else{
      managerData.push( {
        ...val,
        data: [ val.money ]
      } );
    }
  })
  managerData.forEach(val=>{
    val.count = (val.data.reduce((prev,val)=>prev+val, 0)).toFixed( 2 ) - 0;
  })

  // 汇总总经理数据
  const bossData = [];
  boss.forEach(val=>{
    const i = bossData.findIndex(v=>v.name === val.name);
    if(i !== -1){
      bossData[i].data.push( val.money )
    }else{
      bossData.push( {
        ...val,
        data: [ val.money ]
      } );
    }
  })
  bossData.forEach(val=>{
    val.count = (val.data.reduce((prev,val)=>prev+val, 0)).toFixed( 2 ) - 0;
  })

  return {
    staff: staffuData,
    manager: managerData,
    boss: bossData
  };
}